package com.sondon.mayi.adapter;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sondon.mayi.entity.AddressInfo;
import com.sondon.mayi.events.CheckboxEvent;
import com.sondon.mayi.fruit.R;
import com.sondon.mayi.util.PreferencesUtil;

import de.greenrobot.event.EventBus;

@EViewGroup(R.layout.address_item)
public class AddressList_Item extends LinearLayout {
	Context context;
	@ViewById
	TextView receiver;//收货人
	@ViewById
	TextView mobile;//手机号码
	@ViewById
	TextView address;//地址
	@ViewById
	TextView choose;//默认
	@ViewById
	ImageView address_item_myright;//右边箭头
	@ViewById CheckBox address_item_checkbox;//选择框
	
	private int position;
	public AddressList_Item(Context context) {
		super(context);
		this.context=context;
	}
	
	//填充数据
	public void bindData(AddressInfo addressInfo,int position){
		this.position=position;
		receiver.setText(addressInfo.getAccept_name());
		mobile.setText(addressInfo.getMobile());
		
		String city=addressInfo.getCity_name();
		if("市辖区".equals(city)||"县".equals(city)){
			city="";
		}
		address.setText(
				addressInfo.getProvince_name()
				+city
				+addressInfo.getArea_name()
				+addressInfo.getAddress()
				);
		
		//是否默认,0:为非默认,1:默认
		if("0".equals(addressInfo.getIs_default())){
			choose.setVisibility(View.GONE);
		}else if("1".equals(addressInfo.getIs_default())){
			choose.setVisibility(View.VISIBLE);
		}
		
		//是否通过startActivityForResult启动的
		boolean is_forResult=PreferencesUtil.getBooleanValue("is_forResult");
		if(is_forResult){
			address_item_checkbox.setVisibility(View.VISIBLE);
			address_item_myright.setVisibility(View.GONE);
		}else{
			address_item_checkbox.setVisibility(View.GONE);
			address_item_myright.setVisibility(View.VISIBLE);
		}
	}
	
	@Click({R.id.address_item_checkbox})
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.address_item_checkbox:
			EventBus.getDefault().post(new CheckboxEvent(this.position));
			break;

		default:
			break;
		}
	}
}
